package vip.xiaonuo.miniapp.api.listener;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import reactor.core.Reactor;
import reactor.event.Event;
import reactor.function.Consumer;
import vip.xiaonuo.core.exception.ServiceException;
import vip.xiaonuo.facedoor.modular.memberuser.entity.MemberUser;
import vip.xiaonuo.facedoor.modular.memberuser.service.MemberUserService;
import vip.xiaonuo.facedoor.modular.orgdevice.entity.OrgDevice;
import vip.xiaonuo.facedoor.modular.orgdevice.mapper.OrgDeviceMapper;
import vip.xiaonuo.facedoor.modular.topic.EventTopic;
import vip.xiaonuo.facedoor.modular.visitor.entity.Visitor;
import vip.xiaonuo.facedoor.modular.visitor.service.VisitorService;
import vip.xiaonuo.facedoor.modular.visitordevice.entity.VisitorDevice;
import vip.xiaonuo.facedoor.modular.visitordevice.mapper.VisitorDeviceMapper;
import vip.xiaonuo.miniapp.api.miniappmemberuser.entity.MiniappMemberUser;
import vip.xiaonuo.miniapp.api.miniappmemberuser.service.MiniappMemberUserService;
import vip.xiaonuo.sys.modular.org.entity.SysOrg;
import vip.xiaonuo.sys.modular.org.service.SysOrgService;

import javax.annotation.Resource;
import java.util.List;

import static reactor.event.selector.Selectors.$;

/**
 * 访客机新增访客 监听 门禁设备增加
 */
@Service
@Slf4j
public class MemberUpdateListener implements Consumer<Event<MemberUser>>, InitializingBean {


    @Autowired
    private Reactor reactor;


    @Autowired
    private MiniappMemberUserService miniappMemberUserService;

    @Autowired
    private MemberUserService memberUserService;

    @Override
    public void accept(Event<MemberUser> event) {
        MemberUser data = event.getData();
        if (data.getMiniAppMemberId()==null){
            MiniappMemberUser miniappMemberUser = new MiniappMemberUser();
            BeanUtils.copyProperties(data,miniappMemberUser);
            miniappMemberUser.setId(null);
            miniappMemberUser.setStatus(1);
            miniappMemberUserService.save(miniappMemberUser);
            data.setMiniAppMemberId(miniappMemberUser.getId());
            memberUserService.updateById(data);
        }else {
            MiniappMemberUser miniappMemberUser = miniappMemberUserService.getById(data.getMiniAppMemberId());
            if (miniappMemberUser!=null){
                Long miniAppMemberId=miniappMemberUser.getId();
                BeanUtils.copyProperties(data,miniappMemberUser);
                miniappMemberUser.setId(miniAppMemberId);
                miniappMemberUser.setStatus(1);
                miniappMemberUserService.updateById(miniappMemberUser);
            }else {
                miniappMemberUser = new MiniappMemberUser();
                BeanUtils.copyProperties(data,miniappMemberUser);
                miniappMemberUser.setId(null);
                miniappMemberUser.setStatus(1);
                miniappMemberUserService.save(miniappMemberUser);
                data.setMiniAppMemberId(miniappMemberUser.getId());
                memberUserService.updateById(data);
            }
        }
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        reactor.on($(EventTopic.MEMBER_UPDATE), this);
    }
}

